<?php

namespace mbilling\BillingBundle\Security;

use Doctrine\ORM\EntityManager;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;

use mbilling\BillingBundle\Model\UsersModel;
use mbilling\BillingBundle\Entity\users;

class usersProvider implements UserProviderInterface
{
    protected $em;
    
    public function __construct(EntityManager $entityManager)
    {
        $this->em = $entityManager;
    }
    public function loadUserByUsername($username)
    {
        $user_model = new UsersModel($this->em);
        $userData = $user_model->findByLogin($username);
        if ($userData) 
        {
            return $userData;
        } 
        else 
        {
            throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
        }
    }

    public function refreshUser(UserInterface $user)
    {
        if (!$user instanceof users) {
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
        }

        return $this->loadUserByUsername($user->getUsername());
    }

    public function supportsClass($class)
    {
        return $class === 'mbilling\BillingBundle\Entity\users';
    }
}

?>
